package com.template.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.util.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.template.entity.Post;
import com.template.entity.PostComment;
import com.template.mapper.CommunityMapper;
import com.template.result.PageBean;
import com.template.service.CommunityService;
import com.template.vo.PostVO;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.java.Log;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

//@Logger
@Service
public class CommunityServiceImpl implements CommunityService {

    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private ObjectMapper objectMapper;
    @Override
    public PageBean getPostList(Integer pageNo, Integer pageSize,Integer userId) throws JsonProcessingException {
        PageHelper.startPage(pageNo,pageSize);

        List<Post> postList=communityMapper.getPostList();

        List<PostVO> postVOList=new ArrayList<>();
//        System.out.println(postList);

        for(Post post :postList){
            PostVO postVO= BeanUtil.copyProperties(post, PostVO.class);

//            处理点赞列表
//            Collections.emptyList() 是 Java 提供的一个工具方法，返回一个 空的、不可修改的 List 实例。
            List<Integer> likeManList = Collections.emptyList();
            String likeManJson = post.getLikeManList();
            if (StringUtils.isNotBlank(likeManJson)) {
                try {
                    likeManList = objectMapper.readValue(likeManJson,
                            new TypeReference<List<Integer>>() {});
//                    System.out.println(likeManList);
                } catch (JsonProcessingException e) {
                    // 日志记录异常
//                    logger.warn("异常帖子ID: {}", post.getId(), e);
                }
            }

            postVO.setLikeMan(likeManList.size());

            postVO.setIsgood(likeManList.contains(userId));

            postVOList.add(postVO);
//            System.out.println(postVO);
        }
        Page<Post> p=(Page<Post>) postList;

        return new PageBean(p.getTotal(),postVOList);
    }

    @Override
    public PageBean searchPosts(String keyword, Integer pageNo, Integer pageSize, Integer userId) throws JsonProcessingException {
        PageHelper.startPage(pageNo, pageSize);

        List<Post> postList = communityMapper.searchPosts(keyword);

        List<PostVO> postVOList = new ArrayList<>();

        for (Post post : postList) {
            PostVO postVO = BeanUtil.copyProperties(post, PostVO.class);

            // 处理点赞列表
            List<Integer> likeManList = Collections.emptyList();
            String likeManJson = post.getLikeManList();
            if (StringUtils.isNotBlank(likeManJson)) {
                try {
                    likeManList = objectMapper.readValue(likeManJson,
                            new TypeReference<List<Integer>>() {
                            });
                } catch (JsonProcessingException e) {
                    // 日志记录异常
                }
            }

            postVO.setLikeMan(likeManList.size());
            postVO.setIsgood(likeManList.contains(userId));

            postVOList.add(postVO);
        }
        Page<Post> p = (Page<Post>) postList;

        return new PageBean(p.getTotal(),postVOList);
    }

    @Override
    public void addPost(PostVO postVO) throws JsonProcessingException {
        Post post= BeanUtil.copyProperties(postVO, Post.class);
//        System.out.println(postVO);

        List<Integer> likeList=new ArrayList<>();
        String likeManList=objectMapper.writeValueAsString(likeList);

        post.setCreateTime(LocalDateTime.now());
        post.setUpdateTime(LocalDateTime.now());
        post.setIsShow(1);
        post.setLikeManList(likeManList);

//        System.out.println(post);
        communityMapper.addPost(post);

    }

    @Override
    public void isGood(Integer userId, Integer id) {
        Post post1=communityMapper.getPostById(id);

        List<Integer> likeManList =Collections.emptyList();
        String likeManJson=post1.getLikeManList();
        if(StringUtils.isNotBlank(likeManJson)){
            try {
                likeManList = objectMapper.readValue(likeManJson,
                        new TypeReference<List<Integer>>() {});
                likeManList.add(userId);
                String likeManJson1=objectMapper.writeValueAsString(likeManList);
                post1.setLikeManList(likeManJson1);

                communityMapper.updatePostById(post1);
            } catch (JsonProcessingException e) {
                // 日志记录异常
//                    logger.warn("异常帖子ID: {}", post.getId(), e);
            }
        }
        System.out.println(likeManList);
    }

    @Override
    public void isNotGood(Integer userId, Integer id) {
        Post post1=communityMapper.getPostById(id);

        List<Integer> likeManList =Collections.emptyList();
        String likeManJson=post1.getLikeManList();
        if(StringUtils.isNotBlank(likeManJson)){
            try {
                likeManList = objectMapper.readValue(likeManJson,
                        new TypeReference<List<Integer>>() {});
                if(likeManList.contains(userId)){
                    likeManList.remove(userId);
                    String likeManJson1=objectMapper.writeValueAsString(likeManList);
                    post1.setLikeManList(likeManJson1);

                    communityMapper.updatePostById(post1);
                }
            } catch (JsonProcessingException e) {
                // 日志记录异常
//                    logger.warn("异常帖子ID: {}", post.getId(), e);
            }
        }
        System.out.println(likeManList);
    }

    @Override
    public PostVO getPostInfo(Integer postId, Integer userId) throws InvocationTargetException, IllegalAccessException {
        PostVO vo = new PostVO();
        BeanUtils.copyProperties(vo, communityMapper.getPostById(postId));
        System.out.println(vo);
        return vo;
    }

    @Override
    public void addPostComment(PostComment postComment) {
        postComment.setCreateTime(LocalDateTime.now());
        postComment.setUpdateTime(LocalDateTime.now());
        communityMapper.addPostComment(postComment);
    }

    @Override
    public List<PostComment> getPostCommentList(Integer postId) {

        return communityMapper.getPostCommentList(postId);
    }

}
